const App = getApp();
const db = wx.cloud.database();
const _ = db.command;
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前时间
    TimeValue: "",
    // 选择时间的箭头
    addcell: "",
    endcell: "",
    // 判断是开始还是结束的值
    TimeKey: "",
    // 展示时间
    addTime: "",
    endTime: "",
    // 新建实践基地内容
    addPracticeBase: {
      name: "",
      location: "",
      profession: "",
      addTime: null,
      endTime: null,
      ContactPerson: "",
      ContactNumber: null,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this
    let UserInfo = App.cache.getUserInfo()
    console.log('获取用户信息:', UserInfo);
    delete UserInfo._id
    that.setData({
      UserInfo: UserInfo,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 打开选择时间弹窗
  TimePopup(event) {
    // 先更新目前时间
    console.log(App.Time.TimeCode());
    that.setData({
      TimeValue: App.Time.TimeCode(),
    })
    const {
      key
    } = event.currentTarget.dataset
    console.log('key', key);
    if (key == 'add') {
      that.setData({
        addcell: "down",
        endcell: "",
        // 判断开始还是结束的值
        TimeKey: key,
      });
    } else {
      that.setData({
        addcell: "",
        endcell: "down",
        // 判断开始还是结束的值
        TimeKey: key,
      });
    }
  },
  // 选择时间
  TimehandleChange(e) {
    // 获取开始时间还是结束时间
    let key = e.currentTarget.dataset.key
    // 获取标准时间
    let Time = e.detail.dateString
    // 将时间转成时间戳
    let formatTime = new Date(Time).getTime()
    if (key == 'add') {
      that.setData({
        addcell: "",
        // 判断开始还是结束的值
        TimeKey: key,
        addTime: Time,
        'addPracticeBase.addTime': formatTime,
      });
    } else {
      that.setData({
        endcell: "",
        // 判断开始还是结束的值
        TimeKey: key,
        endTime: Time,
        'addPracticeBase.endTime': formatTime
      });
    }
    console.log('开始', that.data.addPracticeBase.addTime);
    console.log('结束', that.data.addPracticeBase.endTime);
  },
  // 获取input输入的值
  handleChange(e, key) {
    const {
      detail,
      currentTarget
    } = e
    console.log('detail', detail);
    console.log('currentTarget', currentTarget);
    this.setData({
      [`addPracticeBase.${currentTarget.id}`]: detail.value,
    })
  },
  // 效验
  Check() {
    let Practice = that.data.addPracticeBase
    console.log('Practice', Practice);
    let ContactNumber = App.checkData.checkData(Practice.ContactNumber, '联系电话')
    let ContactPerson = App.checkData.checkData(Practice.ContactPerson, '联系人名称')
    let profession = App.checkData.checkData(Practice.profession, '实践需求专业')
    let location = App.checkData.checkData(Practice.location, '实践基地位置')
    let name = App.checkData.checkData(Practice.name, '实践基地名称')
    if (!name || !location || !profession || !ContactPerson || !ContactNumber) return
    if (Practice.addTime == null || Practice.endTime == null) {
      App.toast.showToast('时间需填写完整')
      return
    }
    if (Practice.endTime <= Practice.addTime) {
      App.toast.showToast('结束时间不能小于开始时间')
      return
    }
    this.addPracticeBase(Practice)
  },
  // 创建实践基地
  addPracticeBase(data) {
    console.log('data', data);
    App.toast.showLoading('创建中')
    let practiceBaseId = (+new Date()).toString().substring(5, 13)
    db.collection('practiceBase').add({
        data: {
          name: data.name,
          location: data.location,
          profession: data.profession,
          addTime: data.addTime,
          endTime: data.endTime,
          ContactPerson: data.ContactPerson,
          ContactNumber: data.ContactNumber,
          practiceBaseId: 'base' + practiceBaseId,
          creator: {
            name: that.data.UserInfo.nickName,
            account: that.data.UserInfo.account
          },
          remark: '',
          file: [],
          members: [],
          TimeTis: Date.now(),
          permissions: true,
        }
      })
      .then(res => {
        console.log('创建成功', res);
        App.toast.hideLoading()
        App.toast.showToast('创建成功，即将返回')
        setTimeout(() => {
          App.route.relBack('/pages/practiceBase/list/index')
        }, 1500)
      })
  }
})